import { ChatSendTypeEnum, DEFAULT_CHAT_SYSTEM_MESSAGE, STOP_ANSWER } from '@src/component/business/BizChatPanel/chat/model';
/* model */
import { ChatIMMessageItemType, OpenAIMessageItemType, OpenAIMessageRoleEnum } from "@src/component/business/BizChatPanel/chat/model";
/* util */
import { isFalsy } from "@src/util/type";
import { uuid } from '@src/util/lang/string';

/** 
 * @description 判断是否是用户消息
*/
function isUserMessage(message: OpenAIMessageItemType): boolean {
  return message?.role === OpenAIMessageRoleEnum.User;
}

/**
 * @description 判断是否是助理消息
*/
function isAssistantMessage(message: OpenAIMessageItemType): boolean {
  return message?.role === OpenAIMessageRoleEnum.Assistant;
}

/**
 * @description 判断是否是系统消息
*/
function isSystemMessage(message: OpenAIMessageItemType): boolean {
  return message?.role === OpenAIMessageRoleEnum.System;
}

/** 
 * @description 判断是否不为用户消息
*/
function isNotUserMessage(message: OpenAIMessageItemType): boolean {
  return isFalsy(isUserMessage(message));
}

/** 
 * @description 获取系统消息
*/
function getSystemMessageByContent(content: string): OpenAIMessageItemType {
  return {
    role: OpenAIMessageRoleEnum.System,
    content: content,
    id: uuid()
  }
}

/** 
 * @description 获取系统消息
*/
function getSystemMessage(): OpenAIMessageItemType {
  return {
    role: OpenAIMessageRoleEnum.System,
    content: DEFAULT_CHAT_SYSTEM_MESSAGE,
    id: uuid()
  }
}


/** 
 * @description 获取助理消息
*/
function getAssistantMessageByContent(content: string): OpenAIMessageItemType {
  return {
    role: OpenAIMessageRoleEnum.Assistant,
    content,
    id: uuid()
  }
}

/** 
 * @description 获取用户消息
*/
function getUserMessageByContent(content: string): OpenAIMessageItemType {
  return {
    role: OpenAIMessageRoleEnum.User,
    content,
    id: uuid()
  }
}

function getAssistantLoadingMessage(): OpenAIMessageItemType {
  return {
    role: OpenAIMessageRoleEnum.Assistant,
    content: '正在输入...',
    id: uuid()
  }
}

function getAssistantStopMessage(): OpenAIMessageItemType {
  return {
    role: OpenAIMessageRoleEnum.Assistant,
    content: STOP_ANSWER,
    id: uuid(),
    isStop: true
  }
}

function chatIMMessageToAIMessage(message: ChatIMMessageItemType): OpenAIMessageItemType {
  
  const content = message.content
  const createTime = message?.createTime
  const id = message?.id
  const likeStatus = message?.likeStatus
  
  // 系统消息
  if (content == DEFAULT_CHAT_SYSTEM_MESSAGE) {
    return {
      role: OpenAIMessageRoleEnum.System,
      content: content,
      createTime,
      id,
      likeStatus,
      isHistory: true
    }
  }
  
  // 用户消息
  if (message?.type == ChatSendTypeEnum.User) {
    return {
      role: OpenAIMessageRoleEnum.User,
      content: content,
      createTime,
      id,
      likeStatus,
      isHistory: true
    }
  }
  
  // 助理消息
  if (message?.type == ChatSendTypeEnum.Robot) {
    return {
      role: OpenAIMessageRoleEnum.Assistant,
      content: content,
      createTime,
      id,
      likeStatus,
      isHistory: true
    }
  }
  
  throw new Error('未知的消息类型')
}

export {
  getAssistantStopMessage,
  isUserMessage,
  isAssistantMessage,
  isSystemMessage,
  isNotUserMessage,
  getSystemMessageByContent,
  getSystemMessage,
  getAssistantMessageByContent,
  getUserMessageByContent,
  getAssistantLoadingMessage,
  chatIMMessageToAIMessage
}
